package com.jn.zhinongbao.adapter.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class PreferenceManager {
    
    private static final String PREF_NAME = "agricultural_treasures_prefs";
    private static final String KEY_USER_ID = "user_id";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_USER_ROLE = "user_role";
    private static final String KEY_IS_LOGGED_IN = "is_logged_in";

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    public PreferenceManager(Context context) {
        preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        editor = preferences.edit();
    }

    /**
     * 保存用户登录信息
     */
    public void saveUserLogin(int userId, String username, int role) {
        editor.putInt(KEY_USER_ID, userId);
        editor.putString(KEY_USERNAME, username);
        editor.putInt(KEY_USER_ROLE, role);
        editor.putBoolean(KEY_IS_LOGGED_IN, true);
        editor.apply();
    }

    /**
     * 清除用户登录信息
     */
    public void clearUserLogin() {
        editor.clear();
        editor.apply();
    }

    /**
     * 检查用户是否已登录
     */
    public boolean isLoggedIn() {
        return preferences.getBoolean(KEY_IS_LOGGED_IN, false);
    }

    /**
     * 获取当前用户ID
     */
    public int getCurrentUserId() {
        return preferences.getInt(KEY_USER_ID, -1);
    }

    /**
     * 获取当前用户名
     */
    public String getCurrentUsername() {
        return preferences.getString(KEY_USERNAME, "");
    }

    /**
     * 获取当前用户角色
     */
    public int getCurrentUserRole() {
        return preferences.getInt(KEY_USER_ROLE, -1);
    }

    /**
     * 判断当前用户是否是商家
     */
    public boolean isCurrentUserMerchant() {
        return getCurrentUserRole() == 0;
    }

    /**
     * 判断当前用户是否是普通用户
     */
    public boolean isCurrentUserRegularUser() {
        return getCurrentUserRole() == 1;
    }
} 